package com.andy.eqm.server.dto;

import com.andy.eqm.server.socket.codec.IWrite;

import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;

public abstract class BaseMessage implements IWrite{

	protected Integer cmd;
	protected byte[] byteArray;

	@Override
	public void write(ByteBuf buf) {
		if(cmd != null){
			buf.writeInt(cmd);
		}
		writeBody(buf);
	}
	
	@Override
	public byte[] getByteArray(){
		return byteArray;
	}
	
	@Override
	public void setByteArray(byte[] byteArray){
		this.byteArray = byteArray;
	}
	
	@Override
	public Integer getCmd() {
		return cmd;
	}
	
	@Override
	public void setCmd(int cmd){
		this.cmd = cmd;
	}

	public static void writeString(ByteBuf buf,String str){
		int ri = buf.writerIndex();
		buf.writeShort(0);
		int len = buf.writeCharSequence(str, CharsetUtil.UTF_8);
		buf.setShort(ri, len);
	}
	
	
	public abstract void writeBody(ByteBuf buf);
	
	

}
